//
// Created by linyongzhi on 2023/12/11.
//

#include <cstdio>
#include <cstring>
#include <new>
#include "CPHBuffer.h"

int CPHBuffer::Init(uint32_t capacity) {
    if (m_data != nullptr) {
        printf("CPHBuffer::Init has init");
        return -1;
    }
    m_data = new(std::nothrow) uint8_t[capacity];
    if (m_data == nullptr) {
        return -1;
    }
    m_capacity = capacity;
    return 0;
}

int CPHBuffer::FillInData(uint8_t *data, uint32_t dataLen) {
    if (dataLen > m_capacity) {
        printf("CPHBuffer::FillInData dataLen is invalid.\n");
        return -1;
    }

    std::memcpy(m_data, data, dataLen);
    m_dataLen = dataLen;
    return 0;
}

void CPHBuffer::DeInit() {
    if (m_data != nullptr) {
        delete [] m_data;
        m_dataLen = 0;
        m_capacity = 0;
    }
}
